var api = require('../../../config/api.js');
var app = getApp();
var that;
Page({
  data: {
    username: '',
    password: '',
    userinfo:[],
    code:''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    wx.setNavigationBarTitle({
      title: "用户登录中心"
    })
  },
  onReady: function () {
  },
  onShow: function () {
    var userInfo=wx.getStorageSync("userInfo");
    if (userInfo.user_id != "0"){
      wx.redirectTo({
        url: '/pages/ucenter/index/index'//实际路径要写全
      })
    }
  },
  weiXinLogin:function(){
    let that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
         wx.getUserInfo({
            success: function (res1) {
              wx.request({
                url: api.wxLogin,
                data: {
                  nickname: res1.userInfo.nickName,
                  userimg: res1.userInfo.avatarUrl,
                  code: res.code
                },
                success: function (res2) {
                  if (res2.data.errno == 0) {
                    wx.setStorageSync("userInfo", res2.data.data);
                    wx.setStorageSync("userId", res2.data.data.user_id);
                    wx.switchTab({
                      url: '/pages/ucenter/index/index'//实际路径要写全
                    })
                  }
                }
              });
            }
          })
        } 
      },
      fail: function (err) {
        reject(err);
      }
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  startLogin: function () {
    var that = this;
    if (that.data.password.length < 1 || that.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.ApiRootUrl + 'applets.php?act=login',
      data: {
        username: that.data.username,
        password: that.data.password,
        has_code: api.hasCode
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.errno ==0){
          wx.setStorageSync("userInfo", res.data.data);
          wx.setStorageSync("userId", res.data.data.user_id);
          wx.switchTab({
            url: '/pages/ucenter/index/index'//实际路径要写全
          })
        }else{
          wx.showToast({
            image: '/static/images/icon_error.png',
            title:res.data.data,
            mask: true
          })

        }
      }
    });
  },
  bindUsernameInput: function (e) {

    this.setData({
      username: e.detail.value
    });
  },
  bindPasswordInput: function (e) {

    this.setData({
      password: e.detail.value
    });
  },
  bindCodeInput: function (e) {

    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function (e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})